class Manage::UsersController < Manage::MainController
  before_filter :find_user, :only=>[:edit, :update, :destroy, :activate, :suspend, :delete, :unsuspend]
  
  # GET /manage/users
  # GET /manage/users.xml
  def index
    @users = User.paginate(:all, :page=>params[:page], :per_page=>20, :order=>"name")
    
    respond_to do |format|
      format.html # render index.html
      format.xml{ render :xml => @users }
    end
  end
  
  # GET /manage/users/new
  def new
    @user = User.new
  end
  
  # POST /manage/users
  # POST /manage/users.xml
  def create
    @user = User.new(params[:user])
    
    respond_to do |format|
    	if @user.valid? && @user.save && @user.register!
        flash[:notice] = I18n.t('flash.user.created')
        
        format.html { redirect_to manage_users_path }
        format.xml { head :ok }
      else
      	flash.now[:error] = I18n.t('flash.user.create_error')
      	
        format.html { render :action => "new" }
        format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /manage/users/1
  # PUT /manage/users/1.xml
  def update    
    respond_to do |format|
    	if @user.update_attributes(params[:user])
        flash[:notice] = I18n.t('flash.user.updated')
        
        format.html { redirect_to manage_users_path }
        format.xml { head :ok }
      else
      	flash.now[:error] = I18n.t('flash.user.update_error')
      	
        format.html { render :action => "edit" }
        format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /manage/users/1
  # DELETE /manage/users/1.xml
  def destroy
    @user.destroy
    flash[:notice] = I18n.t('flash.user.destroy')
    
    respond_to do |format|
      format.html { redirect_to manage_users_path }
      format.xml { head :ok }
    end
  end
  
  # POST /manage/users/1/activate
  def activate
    @user.activate!
    
    respond_to do |format|
      format.html { redirect_to manage_users_path }
      format.xml { head :ok }
    end
  end
  
  # POST /manage/users/1/suspend
  def suspend
    @user.suspend! 
    
    respond_to do |format|
      format.html { redirect_to manage_users_path }
      format.xml { head :ok }
    end
  end

  # POST /manage/users/1/unsuspend
  def unsuspend
    @user.unsuspend! 
    
    respond_to do |format|
      format.html { redirect_to manage_users_path }
      format.xml { head :ok }
    end
  end

  # POST /manage/users/1/delete
  def delete
    @user.delete!
    
    respond_to do |format|
      format.html { redirect_to manage_users_path }
      format.xml { head :ok }
    end
  end
  
  private
  
  def find_user
    @user = User.find(params[:id])
  end
end
